﻿package com.hhy1001.fireplane;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.MotionEvent;

public class GameMenu {
	//菜单背景图
	private Bitmap bmpMenu;
	//按钮图片资源（未按下时）
	private Bitmap bmpButton;
	//按钮图片资源（按下时）
	private Bitmap bmpButtonPress;
	//按钮坐标
	private int btnX,btnY;
	//按钮是否按下标识
	private Boolean isPress;
	//图片矩形坐标
	private Rect src;
	//屏幕矩形坐标
	private RectF dst;
	//菜单初始化
	public GameMenu(Bitmap bmpMenu, Bitmap bmpButton, Bitmap bmpButtonPress) {
		
		this.bmpMenu = bmpMenu;
		this.bmpButton = Bitmap.createBitmap(bmpButton, 0, 0, bmpButton.getWidth()/3, bmpButton.getHeight());
		this.bmpButtonPress = Bitmap.createBitmap(bmpButton, bmpButtonPress.getWidth()/3, 0, bmpButtonPress.getWidth()/3, bmpButtonPress.getHeight());
		//X居中，Y紧接屏幕底部
		btnX = MyView.screenW / 2 - this.bmpButton.getWidth()/2;
		btnY = MyView.screenH - this.bmpButton.getHeight();
		//初始化图片矩形
		src = new Rect();
		src.left = 0;
		src.top = 0;
		src.right = bmpMenu.getWidth();
		src.bottom = bmpMenu.getHeight();
		//初始化屏幕矩形
		dst = new RectF();
		dst.left = 0;
		dst.top = 0;
		dst.right = MyView.screenW;
		dst.bottom = MyView.screenH;
		isPress = false;
	}
	
	public void draw(Canvas canvas,Paint paint) {
		//绘制菜单背景图
		//canvas.drawBitmap(bmpMenu, 0, 0, paint);
		canvas.drawBitmap(bmpMenu, src, dst, paint);
		if (isPress) {
			canvas.drawBitmap(bmpButtonPress, btnX, btnY,paint);
		}else{
			canvas.drawBitmap(bmpButton, btnX, btnY,paint);
		}
	}
	
	public void onTouchEvent(MotionEvent event) {
		//获取当前触控位置
		int pointX = (int) event.getX();
		int pointY = (int) event.getY();
		
		//当用户按下和移动时
		if (event.getAction() == MotionEvent.ACTION_DOWN || 
				event.getAction() == MotionEvent.ACTION_MOVE) {
			//用户是否点击按钮
			if (pointX > btnX && pointX < btnX + bmpButton.getWidth()) {
				if (pointY > btnY && pointY < btnY+bmpButton.getHeight()) {
					isPress = true;
				}
			} else {
				isPress = false;
			}
			//抬起动作
		}else if(event.getAction() == MotionEvent.ACTION_UP){
			//抬起时是否点击按钮
			if (pointX > btnX && pointX < btnX + bmpButton.getWidth()) {
				if (pointY > btnY && pointY < btnY+bmpButton.getHeight()) {
					isPress = false;//将button状态置为未按下状态
					//改变游戏状态为开始游戏
					MyView.gameState = MyView.GAMEING;
				}
			}
		}
	}
	
	
}
